<?php
namespace app\index\controller;

use app\index\model\Cate;
use app\index\model\Question;
use app\index\model\Sign;
use think\Controller;
use think\Db;

class Index extends Common
{
    public function index()
    {

        // cache('name','xuebingsi',60);

        // var_dump(cache('name'));

        // exit;



    	$where = [];

    	if(input('cid')){
    		$where['t1.cid'] = input('cid');
    	}

    	if(input('type')==1){
    		$where['t1.status'] = 0;
    	}

    	if(input('type')==2){
    		$where['t1.status'] = 1;
    	}

    	if(input('type')==3){
    		$where['is_jing'] = 1;
    	}

    	// $sql = "select count(*) cn,t1.user_id,t2.face,t2.nickname from fly_reply t1 INNER JOIN fly_user t2 on t1.user_id=t2.user_id where t1.create_time>1546319487 GROUP BY t1.user_id ORDER BY cn desc limit 12";

    	// $data = Db::query($sql);
    	
    	$all_data = Db::name('question')
		->field('t1.*,t2.cate_name,t3.nickname,t3.face')
		->alias('t1')
		->join('cate t2','t1.cid = t2.cate_id')
        ->join('user t3','t1.user_id = t3.user_id')
        ->order('t1.create_time desc')
        ->where($where)
		->paginate(5);

    	$top = Db::name('question')
		->field('t1.*,t2.cate_name,t3.nickname,t3.face')
		->alias('t1')
		->join('cate t2','t1.cid = t2.cate_id')
        ->join('user t3','t1.user_id = t3.user_id')
		->where('t1.is_top','1')
        // ->where('delete','null')
		->order('t1.create_time desc')
        ->cache(true,86400)
		->select();

    	$week_reply = Db::name('reply')
		->field('count(*) cn,t1.user_id,t2.face,t2.nickname')
		->alias('t1')
        ->join('user t2','t1.user_id = t2.user_id')
		->where('t1.create_time','>',strtotime(date('Y-m-d'))-7*86400)
		->group("t1.user_id")
		->order('cn desc')
        ->cache(true,3600)
		->limit(12)
		->select();

    	$list =  Question::where('create_time','>',strtotime(date('Y-m-d'))-7*86400)->order('reply_num desc')->limit(10)->cache(true,3600)->select();

    	$cates = Cate::cache(true,86400)->select();


        $sign = (new Sign)->getSignInfo();

        $this->assign('sign',$sign);
    	$this->assign('cates',$cates);
    	$this->assign('list',$list);
    	$this->assign('week_reply',$week_reply);
    	$this->assign('top',$top);
    	$this->assign('all_data',$all_data);
        $this->assign('title',"首页");

       return $this->fetch();
    }
    public function show()
    {
    	echo 'show';
    }
}
